package com.zp.controller;

import com.zp.pojo.User;
import com.zp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author:周鹏
 * @date: 2020/12/17 11:18
 * @description:
 */
@Controller
public class UserController {
    @Autowired
    @Qualifier("UserServiceImp")
    private UserService userService;
    @RequestMapping("/login_show")
    public String login_show(){
        return "adminlogin";

    }

    /*
       接收前端表格传来user数据
       并执行查询校验
     */
    @RequestMapping("/login")
    public String loginpage(@RequestParam(value = "uname", required = false)String uname, @RequestParam(value = "upassword", required = false)String upassword ,HttpSession session, ModelAndView modelandview){
        User user =new User();
        user.setUname(uname);
        user.setUpassword(upassword);
        System.out.println(user.toString());
        if(uname!=null&&upassword!=null){
            System.out.println("sss");
            User user1=userService.findAllUser(user);
            HashMap<String,Integer> map= new HashMap<String,Integer>();
            map.put("StartPage",1);
            map.put("PageSize",2);
            List<User> list =userService.getUserBypage(map);
            for (User user2:list
                    ) {
                System.out.println(user2);

            }
            if(user1==null) {
                modelandview.addObject("error","用户名或密码不正确");
//                session.setAttribute("error","用户名或密码不正确");
                System.out.println("qqq");
                return "adminlogin";
            }
            //将user内容存在会话中
            session.setAttribute("AdminUser",user1);
            return "login";
        }
        return "adminlogin";
    }
    @RequestMapping("/")
    public String welcome(){
        return "adminlogin";

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        System.out.println("dsadasd");
      session.removeAttribute("AdminUser");
    //  session.invalidate();
        System.out.println("sdasda");
        return "adminlogin";
    }
    @RequestMapping("/getUserByPage")
    public void getuserByLimit (){
        HashMap<String,Integer> map= new HashMap<String,Integer>();
        map.put("StartPage",1);
        map.put("PageSize",1);
        List<User> list =userService.getUserBypage(map);
        for (User user:list
                ) {
            System.out.println(user);

        }

    }
    @RequestMapping("/getList")
    @ResponseBody
    public List<User> getuserList(){
        List<User> list=userService.getuserList();
        return list;
    }
    @RequestMapping("/loginmanager")
    public String loginmanager(){
        return "loginmanager";

    }

}
